package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.model.member.MemberCatalog;
import com.usian.mapper.MemberCatalogMapper;
import com.usian.service.MemberCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: 2210A-project1
 * @package: com.usian.service.impl
 * @className: MemberCatalogServiceImpl
 * @author: 莫洪维
 * @email: 1204315800@qq.com
 * @description: TODO
 * @date: 2023/8/25 20:39
 */
@Service
public class MemberCatalogServiceImpl extends ServiceImpl<MemberCatalogMapper, MemberCatalog> implements MemberCatalogService {

    @Autowired(required = false)
    private MemberCatalogMapper memberCatalogMapper;

    @Override
    public Integer saveMemberCatalog(MemberCatalog memberCatalog) {
        //调用方法进行判断元素是否完整
        if (null == memberCatalog.getCatalogId() || null == memberCatalog.getMemberId()){
            return 0;
        }
        //调用方法进行添加
        int i = memberCatalogMapper.insert(memberCatalog);
        return i;
    }

    @Override
    public List<Long> findByMemberId(Long memberId,Long courseId) {
        //调用方法进行查询
        QueryWrapper<MemberCatalog> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MemberCatalog::getMemberId,memberId);
        wrapper.lambda().eq(MemberCatalog::getCourseId,courseId);
        List<Long> catalogIds = memberCatalogMapper.selectList(wrapper).stream().map(MemberCatalog::getCatalogId).collect(Collectors.toList());
        return catalogIds;
    }

    @Override
    public Integer delCatalogId(Long memberId, Long catalogId,Long courseId) {
        //调用方法进行删除
        QueryWrapper<MemberCatalog> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MemberCatalog::getCatalogId,catalogId);
        wrapper.lambda().eq(MemberCatalog::getMemberId,memberId);
        wrapper.lambda().eq(MemberCatalog::getCourseId,courseId);
        int i = memberCatalogMapper.delete(wrapper);
        return i;
    }
}
